layui.define(function (exports) {
    var $ = layui.$
        , layer = layui.layer
        , admin = layui.admin

    //公共业务的逻辑处理可以写在此处，切换任何页面都会执行
    //……

    // 弹出窗口
    admin.events.popWin = function () {
        var idx = layer.open({
            title: '在线调试'
            , content: '可以填写任意的layer代码'
            , shade: 0
            ,area: ['500px', '300px']
            ,offset:'rb'
        });
        layer.style(idx, {
            right: '50px',
            bottom: '10px',
            marginTop: '-10px',
            marginLeft: '-50px'
        });
    }

    //全屏
    admin.events.fullScreen = function () {
        var ele = document.documentElement
            , reqFullScreen = ele.requestFullScreen || ele.webkitRequestFullScreen
                || ele.mozRequestFullScreen || ele.msRequestFullscreen;
        if (typeof reqFullScreen !== 'undefined' && reqFullScreen) {
            reqFullScreen.call(ele);
        };
        admin.events.hideMenu();
    };
    //隐藏菜单
    admin.events.hideMenu = function () {
        console.log('fullScreen')
        $(".layui-side-menu,.layadmin-pagetabs").css("display", "none");
        $(".layui-header").css({
            top: -100,
            left: 0
        });
        $("#LAY_app_body").css({
            top: 0,
            left: 0
        })
        $("#msgBtn").addClass("floatBox")
        admin.isFullscreen = true
    }
    //显示菜单
    admin.events.showMenu = function () {
        $(".layui-side-menu,.layadmin-pagetabs").css("display", "block");
        $(".layui-header").css({
            top: 0,
            left: 0
        });
        $("#LAY_app_body").removeAttr("style")
        $("#msgBtn").removeClass("floatBox")
    }
    //退出全屏
    admin.events.exitScreen = function () {
        // console.log('$(window) :', $(window));
        if (document.exitFullscreen) {
             document.exitFullscreen();
         }
         else if (document.mozCancelFullScreen) {
             document.mozCancelFullScreen();
         }
        else if (document.webkitExitFullscreen) {
             document.webkitExitFullscreen();
         }
         else if (document.msExitFullscreen) {
             document.msExitFullscreen();
         }
         admin.events.showMenu();
         return this;
    }
    
    // $(window).resize(function () {
    //     console.log('窗口大小改变了')
    //     // console.log('window.onresize :', $(window).resize);
    //     // if (isFullscreenForNoScroll()) {
    //     //     admin.events.hideMenu()
    //     //     $("#LAY_app").append(`
    //     //         <div class="windowedBtn" onClick="layui.admin.events.exitScreen();"><i class="layui-icon">&#xe758;</i></div>
    //     //     `)
    //     //     console.log("我是全屏状态")
    //     // } else {
    //     //     admin.events.showMenu()
    //     //     $(".windowedBtn").remove()
    //     //     console.log("我不是全屏状态")
    //     // }
    // });
    // 监听窗口大小变化
    window.onresize = function() {
        if (isFullscreenForNoScroll()) {
            admin.events.hideMenu()
            $("#LAY_app").append(`
                <div class="windowedBtn" onClick="layui.admin.events.exitScreen();"><i class="layui-icon">&#xe758;</i></div>
            `)
        } else {
            admin.events.showMenu()
            $(".windowedBtn").remove()
        }
    }
    function isFullscreenForNoScroll() {
        return document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen;
    }
    
    //对外暴露的接口
    exports('frame', {});
});